import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_page/community_business/post_detail_page/community_image_page.dart';
import 'package:xvideos/hj_page/user_center_page/widget/gradient_text.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/widget/net_image_widget.dart';
import 'package:xvideos/tools_base/widget/scroll_positioned_list/src/item_positions_listener.dart';
import 'package:xvideos/tools_base/widget/scroll_positioned_list/src/scrollable_positioned_list.dart';

import 'album_action_widget.dart';
import 'pics_detail_logic.dart';

//图集详情页
class PicsDetailPage extends StatelessWidget {
  final String? id;

  PicsDetailPage({Key? key, this.id}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: GetBuilder(
        init: PicsDetailLogic(id),
        tag: "$id${DateTime.now().millisecondsSinceEpoch}",
        builder: (logic) {
          if (logic.videoModel == null) return LoadingCenterWidget();
          if (logic.videoModel?.id == null)
            return CErrorWidget(
              retryOnTap: () => logic.reloadData(),
            );
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildHeader(logic),
              Expanded(child: () {
                if (logic.videoModel?.picLockType == 0) {
                  return ScrollablePositionedList.builder(
                      itemPositionsListener: logic.itemPositionsListener,
                      itemCount: logic.videoModel?.seriesCover?.length ?? 0,
                      itemScrollController: logic.itemScrollCtr,
                      itemBuilder: (context, index) {
                        String? imageUrl = logic.videoModel?.seriesCover?[index];
                        return GestureDetector(
                          onTap: () {
                            Get.to(CommunityImagePage(
                              videoModel: logic.videoModel,
                              index: index,
                            ));
                          },
                          child: NetworkImageLoader(
                            imageUrl: imageUrl,
                            borderRadius: 0,
                            compress: true,
                          ),
                        );
                      });
                } else {
                  return CustomScrollView(
                    slivers: [
                      SliverPadding(
                        padding: EdgeInsets.symmetric(horizontal: 16),
                        sliver: SliverGrid(
                          delegate: SliverChildBuilderDelegate(
                            (context, index) {
                              return NetworkImageLoader(
                                imageUrl: logic.videoModel!.seriesCover![index],
                                compress: true,
                              );
                            },
                            childCount: min(9, logic.videoModel?.seriesCover?.length ?? 0),
                          ),
                          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 3,
                            mainAxisSpacing: 5,
                            crossAxisSpacing: 5,
                            childAspectRatio: 1,
                          ),
                        ),
                      ),
                      SliverToBoxAdapter(
                        child: Column(
                          children: [
                            12.sizeBoxH,
                            Text(
                              '全本完整作品共${logic.videoModel?.seriesCover?.length ?? 0}张',
                              style: textStyle(14, Colors.white, FontWeight.w400),
                            ),
                            12.sizeBoxH,
                            InkWell(
                              onTap: () => logic.showBuyAction(),
                              child: logic.videoModel?.picLockType == 1
                                  ? Text(
                                      '${logic.videoModel?.originCoins ?? 0}金币解锁',
                                      style: TextStyle(
                                        fontSize: 14,
                                        color: Color(0xffFF9000),
                                        fontWeight: FontWeight.w500,
                                        decoration: TextDecoration.underline,
                                        decorationColor: Color(0xffFF9000),
                                      ),
                                    )
                                  : GradientText(
                                      '开通会员解锁',
                                      colors: [
                                        Color(0xffFFE580),
                                        Color(0xffFACC15),
                                      ],
                                      decoration: TextDecoration.underline,
                                      decorationColor: Color(0xffFFE580),
                                      begin: Alignment.topCenter,
                                      end: Alignment.bottomCenter,
                                    ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  );
                }
              }()),
              ValueListenableBuilder<Iterable<ItemPosition>>(
                valueListenable: logic.itemPositionsListener.itemPositions,
                builder: (context, value, child) {
                  return AlbumActionWidget(
                    videoModel: logic.videoModel,
                    currentIndex: logic.videoModel?.picLockType != 0
                        ? -1
                        : value.length > 0
                            ? value.first.index
                            : 1,
                  );
                },
              )
            ],
          );
        },
      )),
    );
  }

  //头部信息
  _buildHeader(PicsDetailLogic logic) {
    return Row(children: [
      InkWell(
        onTap: () => Get.back(),
        child: Icon(Icons.arrow_back_ios_new, color: Colors.white, size: 24),
      ),
      4.sizeBoxW,
      Expanded(
        child: Text(
          '${logic.videoModel?.title}',
          style: TextStyle(color: Colors.white.withOpacity(.9), fontSize: 16, fontWeight: FontWeight.w800),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      )
    ]).paddingSymmetric(horizontal: 16, vertical: 12);
  }
}
